package fr.uniscol.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class Utils {

	public static int getParameterInteger(String nom, Map<String, Object> params, int defaut){
		int val = defaut;
		try{
			val = Integer.parseInt(params.get(nom).toString());
			
		}catch(Exception e){
			return defaut;
		}
		
		return val;
	}
	
	public static int getParameterInteger(String nom, HttpServletRequest request, int defaut){
		int val = defaut;
		try{
			val = Integer.parseInt(request.getParameter(nom));
			
		}catch(Exception e){
			e.printStackTrace();
			return defaut;
		}
		
		return val;
	}
	
	public static Date getParameterDate(String nom, Map<String, Object> params, String format){
		Date d = null;
		try{
			SimpleDateFormat df = new SimpleDateFormat(format);
			
			d = df.parse(params.get(nom).toString());
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return d;
	}
	
	public static Date getParameterDate(String nom, HttpServletRequest request, String format){
		Date d = null;
		try{
			SimpleDateFormat df = new SimpleDateFormat(format);
			
			d = df.parse(request.getParameter(nom));
			
		}catch(Exception e){
			return null;
		}
		return d;
	}
}
